package com.cangshi.controller;

import com.alibaba.fastjson.JSON;
import com.cangshi.entity.JsonObject;
import com.cangshi.exception.JSONException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * Created by Eoly on 2017/4/7.
 */
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public JsonObject handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.setError(400);
        jsonObject.setMes(e.getMessage());
        return jsonObject;
    }

    /**
     * 405 - Method Not Allowed
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public JsonObject handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.setError(405);
        jsonObject.setMes(e.getMessage());
        return jsonObject;
    }

    /**
     * 415 - Unsupported Media Type
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public JsonObject handleHttpMediaTypeNotSupportedException(Exception e) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.setError(415);
        jsonObject.setMes(e.getMessage());
        return jsonObject;
    }



    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(JSONException.class)
    public JsonObject handleJSONException(JSONException e) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.setError(e.getError());
        jsonObject.setMes(e.getMessage());
        return jsonObject;
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(RuntimeException.class)
    public JsonObject handleRuntimeException(RuntimeException e) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.setError(-99);
        jsonObject.setMes(e.getMessage());
        return jsonObject;
    }

    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public JsonObject handleException(Exception e) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.setError(500);
        jsonObject.setMes(e.getMessage());
        return jsonObject;
    }
}
